package com.visual.luckydemo;

import android.content.Context;
import android.graphics.Paint;
import android.text.format.Time;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class StrUtil
{
    final static String TAG = StrUtil.class.getSimpleName();

    public static boolean isNullOrEmpty(String value)
    {
        return (value == null) || (value.length() == 0) || (value.equals("null"));
    }

    public static String[] concat(String[] a, String[] b)
    {
        String[] c = new String[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    public static String getHourMinStr(Time time)
    {
        if (time != null)
        {
            return String.format("%02d", time.hour) + ":" + String.format("%02d", time.minute);
        }
        else
        {
            return "00:00";
        }
    }

    public static void parseTime(final String strHourMin, final String todayDate, Time time, boolean bNow)
    {
        if (time == null)
        {
            time = new Time();
            time.setToNow();
        }

        //存入日期参数
        if (isNullOrEmpty(todayDate))
        {
            if (time != null && bNow)
            {
                time.setToNow();
            }
        }
        else
        {
            if (-1 != todayDate.indexOf("-"))
            {
                if (bNow) // 先把年月日设置好，然后再修改小时和分钟
                {
                    time.setToNow();
                }
                String date[] = todayDate.split("-");
                if (date.length > 0)
                {
                    time.year = StrUtil.parseInt(date[0]);
                    if (date.length > 1)
                    {
                        time.month = StrUtil.parseInt(date[1]) + 1;
                        if (date.length > 2)
                        {
                            time.monthDay = StrUtil.parseInt(date[2]);
                        }
                        else
                        {
                            time.setToNow();
                        }
                    }
                    else
                    {
                        time.setToNow();
                    }
                }
                else
                {
                    time.setToNow();
                }
            }
        }

        //存入时间参数
        if (isNullOrEmpty(strHourMin))
        {
            if (time != null && bNow)
            {
                time.setToNow();
            }
        }
        else
        {
            if (-1 != strHourMin.indexOf(":"))
            {
                if (bNow) // 先把年月日设置好，然后再修改小时和分钟
                {
                    time.setToNow();
                }
                String timer[] = strHourMin.split(":");
                if (timer.length > 0)
                {
                    time.hour = StrUtil.parseInt(timer[0]);
                    if (timer.length > 1)
                    {
                        time.minute = StrUtil.parseInt(timer[1]);
                    }
                    else
                    {
                        time.minute = 0;
                    }
                    time.second = 0;
                }
                else
                {
                    time.setToNow();
                }
            }
        }
    }

    public static void parseHourMin(final String strHourMin, Time time, boolean bNow)
    {
        if (time == null)
        {
            time = new Time();
            time.setToNow();
        }
        if (isNullOrEmpty(strHourMin))
        {
            if (time != null && bNow)
            {
                time.setToNow();
            }
        }
        else
        {
            if (-1 != strHourMin.indexOf(":"))
            {
                if (bNow) // 先把年月日设置好，然后再修改小时和分钟
                {
                    time.setToNow();
                }
                time.hour = StrUtil.parseInt(strHourMin.substring(0, strHourMin.indexOf(":")));
                time.minute = StrUtil.parseInt(strHourMin.substring(strHourMin.indexOf(":") + 1, strHourMin.length()));
                time.second = 0;
            }
        }
    }

    public static String trimUrlTail(String url)
    {
        if (isNullOrEmpty(url))
        {
            return url;
        }
        int i = url.indexOf("?");
        if (i != -1)
        {
            return url.substring(0, i);
        }
        return url;
    }

    public static boolean equalAndNotNull(final String a, final String b)
    {
        if (isNullOrEmpty(a) || isNullOrEmpty(b))
        {
            return false;
        }
        if (a.equals(b))
        {
            return true;
        }
        return false;
    }
    
    public static Long parseLong(final String string)
    {
        try
        {
            return Long.valueOf(string);
        }
        catch (NumberFormatException e)
        {
            LogUtil.Loge(TAG, "parseLong: NumberFormatException string = " + string);
            return 0L;
        }
        catch (Exception e)
        {
            LogUtil.Loge(TAG, "parseLong: Exception string = " + string);
            return 0L;
        }
    }

    public static int parseInt(final String string)
    {
        try
        {
            return Integer.parseInt(string);
        }
        catch (NumberFormatException e)
        {
            LogUtil.Loge(TAG, "parseInt: NumberFormatException string = " + string);
            return 0;
        }
        catch (Exception e)
        {
            LogUtil.Loge(TAG, "parseInt: Exception string = " + string);
            return 0;
        }
    }

    public static int parseInt(final String string, final int radix)
    {
        try
        {
            return Integer.parseInt(string, radix);
        }
        catch (NumberFormatException e)
        {
            LogUtil.Loge(TAG, "parseInt: NumberFormatException string = " + string + " radix = " + radix);
            //LogUtil.handleUncaughtException(LiveVideoApplication.getInstance(), e);
            return 0;
        }
        catch (Exception e)
        {
            LogUtil.Loge(TAG, "parseInt: Exception string = " + string + " radix = " + radix);
            //LogUtil.handleUncaughtException(LiveVideoApplication.getInstance(), e);
            return 0;
        }
    }

}
